package router

import (
	"car-trade-mgr/handler"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Init() *gin.Engine {
	// 1.创建路由
	r := gin.Default()
	r.Use(Cors()) //默认跨域
	r.POST("user_login", handler.UserLogin)
	r.POST("role_add_node", handler.RoleAddNode)
	r.GET("user_role_list", handler.UserRoleList)
	r.GET("role_node_list", handler.RoleNodeList)
	r.POST("car_add", handler.CarAdd)
	r.POST("mgr_add", handler.MgrAdd)
	r.GET("car_mgr_list", handler.CarMgrList)
	r.GET("car_mgr_detail", handler.CarMgrDetail)
	//车辆数据信息管理
	r.POST("user1_login1", handler.User1Login1)
	r.POST("role1_add_node1", handler.Role1AddNode1)
	r.GET("user1_role1_list", handler.User1Role1List)
	r.GET("role1_node1_list", handler.Role1Node1List)
	r.POST("car1_add", handler.AddCar1)
	r.POST("car1_type_add", handler.Car1Type)
	r.GET("car_carType_list", handler.CarCarTypeList)
	r.GET("car_carType_detail", handler.CarCarTypeDetail)
	return r

}
func Cors() gin.HandlerFunc {

	return func(c *gin.Context) {
		method := c.Request.Method
		origin := c.Request.Header.Get("Origin")
		if origin != "" {
			c.Header("Access-Control-Allow-Origin", "*")
			c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
			c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization")
			c.Header("Access-Control-Allow-Credentials", "true")
			c.Set("content-type", "application/json")
		}
		//放行所有OPTIONS方法
		if method == "OPTIONS" {
			c.AbortWithStatus(http.StatusNoContent)
		}
		c.Next()
	}
}
